package com.tencent.qcloud.tim.uikit.component;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.library.common.util.JsonUtil;
import com.library.common.widget.BGButton;
import com.tencent.qcloud.tim.uikit.R;
import com.tencent.qcloud.tim.uikit.base.IGoodsLayout;
import com.tencent.qcloud.tim.uikit.base.INoticeLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.input.InputLayout;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfoUtil;

public class GoodsLayout extends ConstraintLayout implements IGoodsLayout, View.OnClickListener {

    private TextView mTitle;
    private TextView mPrice;
    private ImageView picImage;
    private BGButton btnSend;
    private ImageView ivClose;
    private MessageHandler mMessageHandler;
    private ChatInfo.GoodInfoBean goodsInfo;

    public GoodsLayout(Context context) {
        super(context);
        init();
    }

    public GoodsLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GoodsLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.chat_goods_layout, this);
        mTitle = findViewById(R.id.tvTitle);
        mPrice = findViewById(R.id.tvPrice);
        picImage = findViewById(R.id.ivGoods);
        btnSend = findViewById(R.id.btnSend);
        ivClose = findViewById(R.id.ivClose);
        btnSend.setOnClickListener(this);
        ivClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnSend) {
            if (goodsInfo != null && mMessageHandler != null) {
                MessageInfo info = MessageInfoUtil.buildCustomMessage(JsonUtil.toJson(goodsInfo));
                mMessageHandler.sendMessage(info);
            }
        } else if (v.getId() == R.id.ivClose) {
            setVisibility(GONE);
        }
    }

    public interface MessageHandler {
        void sendMessage(MessageInfo msg);
    }

    public void setMessageHandler(MessageHandler handler) {
        this.mMessageHandler = handler;
    }

    @Override
    public void setGoodInfo(ChatInfo.GoodInfoBean goodsInfo) {
        this.goodsInfo = goodsInfo;
        mTitle.setText(goodsInfo.getGoodName());
        mPrice.setText(goodsInfo.getPrice());
        Glide.with(getContext()).asBitmap().load(goodsInfo.getThumbnail()).centerCrop().placeholder(ContextCompat.getDrawable(getContext(), R.drawable.default_image)).into(picImage);
    }
}
